package com.itheima.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author 王加鑫
 * @Date 2021/07/27 21:17
 * @Since JDK 1.8
 * 异常处理器
 */
/*@Component
@ControllerAdvice*/
public class ExceptionAdvice {


    // 类中定义的方法携带@ExceptionHandler注解的会被作为异常处理器，后面添加实际处理的异常类型
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public String doNullException(Exception ex){
        ex.printStackTrace();
        return "空指针异常";
    }


    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public String doArithmeticException(Exception ex) {
        return "ArithmeticException";
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String doException(Exception ex) {
        ex.printStackTrace();
        return "未知异常";
    }


}
